/* Sentinel - IRC Statistical and Operator Services
** proto.h - Function declarations
**
** Copyright W. Campbell and others.  See README for more details
** Some code Copyright: Jonathan George, Kai Seidler, ircd-hybrid Team,
**                      IRCnet IRCD developers.
**
** $Id: proto.h,v 1.258 2004/05/19 19:26:01 wcampbel Exp $
*/

#ifndef PROTO_H
#define PROTO_H

#include "stats.h"
#include "support.h"

/* Macros that were in m_stats.h */
#define DecreaseOpers(x)        { (x)->opers--; netinfo.opers--; }
#define DecreaseUsers(x)        { (x)->users--; netinfo.users--; }
#define DecreaseServers()       netinfo.servers--;
#define DecreaseChannels()	netinfo.channels--;

/* Macros to replace ugly inlines */
#define msgtoken(x,y)		generictoken(' ', MAXPARA, (x), (y))
#define tabtoken(x,y)		generictoken('\t', MAXPARA, (x), (y))

#define IsStOperOnly() (StatServ.flags & STFLAG_OPERONLY)
#define IsAOnlyOpers()  (settings.flags & FLAG_AONLYOPERS)
#define IsHtml()	(settings.flags & FLAG_HTML)
#define IsDump()	(settings.flags & FLAG_DUMP)
#define IsStSetOper()	(StatServ.flags & STFLAG_SETOPER)
#define IsFlood()	(settings.flags & FLAG_FLOOD)
#define IsVHost()	(settings.pflags & PFLAG_VHOST)
#define IsSilent()	(settings.flags & FLAG_SILENT)
#define IsNotifyEOB()	(settings.flags & FLAG_NOTIFYEOB)
#define IsUnauthChat()	(settings.flags & FLAG_UNAUTHCHAT)
#define IsDCCOperOnly()	(settings.flags & FLAG_DCCOPERONLY)
#define IsPubDelayed()	(settings.flags & FLAG_PUBDELAYED)
#define IsSpyWallops()	(settings.flags & FLAG_SPYWALLOPS)
#define IsOpersAdminOnly()	(settings.flags & FLAG_OPERSADMINONLY)
#define IsMonNickGecos()	(settings.flags & FLAG_MON_NG)
#define IsTrackFizzer()		(settings.flags & FLAG_TRACKFIZZER)
#define IsIgnoreIllegalTLD()	(settings.flags & FLAG_IGNORE_ILLEGAL_TLD)
#define IsOper(u)	((u)->flags & UFLAG_ISOPER)
#define IsAway(u)	((u)->flags & UFLAG_ISAWAY)
#define IsInvis(u)	((u)->flags & UFLAG_INVIS)
#define IsFloodSent(u)	((u)->flags & UFLAG_FLOODSENT)
#define IsOperOn(myu)	((myu)->flags & MFLAG_ISON)
#define IsOperDCC(myu)	((myu)->flags & MFLAG_DCC)
#define IsOperDying(myu)	((myu)->flags & MFLAG_DYING)
#define IsDebug()	(me.cmdflags & CMD_DEBUG)
#define IsNoFork()	(me.cmdflags & CMD_NOFORK)
#define IsSHide(s)	((s)->flags & SFLAG_HIDE)
#define IsAllOperJupe()	(settings.flags & FLAG_JUPE_ALLOPER)

#define IsSPEnableText()	(sp.flags & SPF_ENABLE_TEXT)
#define IsSPEnableExpire()	(sp.flags & SPF_EXPIRE)
#define IsSPEnableRemind()	(sp.flags & SPF_REMIND)
#define IsSPReport()	(sp.flags & SPF_REPORT)
#define IsSPHide()	(sp.flags & SPF_HIDE)
#define IsSPOperOnly()	(sp.flags & SPF_OPERONLY)
#define IsSPTrackOnDisconnect()	(sp.flags & SPF_TRACKONDISCONNECT)

#define SetEOB()	(settings.flags |= FLAG_EOB)
#define IsSetEOB()	(settings.flags & FLAG_EOB)
#define UnSetEOB()	(settings.flags &= ~FLAG_EOB)

#define IsSetOperAdmin(m)	((m) && ((m)->flags & MFLAG_ADMIN))
#define IsSetOperStats(m)	((m) && ((m)->flags & MFLAG_STATS))
#define IsSetOperSplit(m)	((m) && ((m)->flags & MFLAG_SPLIT))
#define IsSetOperJupe(m)	((m) && ((m)->flags & MFLAG_JUPE))
#define IsSetOperList(m)	((m) && ((m)->flags & MFLAG_LIST))
#define IsSetOperOp(m)		((m) && ((m)->flags & MFLAG_OP))
#define IsSetOperTakeover(m)	((m) && ((m)->flags & MFLAG_TAKEOVER))

#define IsVisibleChannel(c)	(!((c)->modes & CMODE_PRIV) && !((c)->modes & CMODE_SEC))

/* External variables... */
extern int servsock;
extern struct Connection *sconn;
extern int need_rehash;
extern int terminate_read_loop;
extern time_t CurrentTime;

extern const char version[];

#ifdef SUPPORT_MSG_UH
#ifdef STATSERV
extern char s_UH[MAXUSER + MAXHOST + 2];
#endif
#ifdef SPLITSERV
extern char sp_UH[MAXUSER + MAXHOST + 2];
#endif
#ifdef JUPESERV
extern char j_UH[MAXUSER + MAXHOST + 2];
#endif
#ifdef LISTSERV
extern char l_UH[MAXUSER + MAXHOST + 2];
#endif
extern char a_UH[MAXUSER + MAXHOST + 2];
#ifdef MONSERV
extern char m_UH[MAXUSER + MAXHOST + 2];
#endif
#endif

#ifdef SUPPORT_MSG_OPERS
extern char s_OPERS[MAXHOST + 7];
#endif

#if defined(SPLITSERV)
int is_delayed;
#endif
#if defined(SPYMODE) || defined(ALERTMODE) || defined(DRONES_CHANNEL)
int ad_delayed;
#endif
#if defined(TRACK_CTCPVERSION)
int mn_delayed;
dlink_list delayed_users;
#endif

#ifdef SPLITSERV
struct SpServer* map_head;
dlink_list split_table;
dlink_list server_table;

/* split-util.c */
void dump_map();
void html_map(FILE *, struct SpServer*, int, int *);
struct mapreturn PrintMap(char*, struct SpServer*, int, int *);
void PrintSplit(char*);
void PrintDump(char *);
void expire_splits(void);

/* split-data.c */
void SpInitTables();
struct SpServer* SpNewServer(char *, char *);
struct SpServer* SpFindServer(char *);
void SpDelServer(char *);
void SpNewSplit(char *);
void SpDelSplit(char *);
struct SpServer* SpFindSplit(char *);

/* splitserv.c */
void splitserv(struct User *, char *, char *);

#endif

/* main.c */
void ServTermination(void);

/* adminserv.c */
void adminserv(struct User *, char *);

/* sock.c */
int ConnectTo(char *, int);
void read_loop();
void close_dccs(void);
struct hostent *get_hostname(char *);

/* send.c */
int sts(char *, ...);
int sendto_dcc(int, char *, ...);
void sendto_all_dcc(char *, ...);
void sendto_partyline(struct Connection *, char *, ...);
void sendto_dcc_flag(int, char *, ...);
void send_dcc_help_file(int, char *, char *);

/* util.c */
char *timediff(time_t);
char *sctime(time_t);
char *sftime(time_t);
char *irc_time(time_t);
#if defined(IRCD_HYBRID) || defined(IRCD_HYB6) || defined(IRCD_HYB7) || \
    defined(IRCD_HYBSVS) || defined(IRCD_BAHAMUT)
int sjtoken(char *, char **);
#endif
unsigned long HASH(const unsigned char *, int);
char *VersionFlags();
#ifdef OPER_MODES
char opermode(struct User *);
#endif
void SigTerm(int);
void SigUsr1(int);
void SigHup(int);
void SigInt(int);
int is_channel(char *);
void alert(char *, ...);
#ifdef DRONES_CHANNEL
void dronealert(char *, ...);
#endif
void spy(char *, ...);
void debug(char *, ...);
void InitLists(void);
int is_midnight(void);
void select_delay(void);

/* conf.c */
void InitDefaults();
void VerifyConf();
void ConfLoad();
void rehash();

/* init.c */
int login();
#ifdef STATSERV
void init_statserv();
#endif
#ifdef STATSERV
void init_join();
#endif
void init_admin();
#ifdef SPLITSERV
void init_splitserv();
void init_spjoin();
#endif
#ifdef JUPESERV
void init_jupeserv();
#endif
#ifdef LISTSERV
void init_listserv();
#endif
#ifdef MONSERV
void init_monserv();
#endif
#ifdef SPYMODE
void init_spy_join();
#endif
#ifdef ALERTMODE
void init_alert_join();
#endif
#ifdef DRONES_CHANNEL
void init_drone_join();
#endif
void InitMessageStrings();
#ifdef PUBSTATS
void init_pubjoin();
#endif

/* ircdsup.c */
void introduce_nick(char *, char *, char *, char *, char *);
void set_oper(struct User *, unsigned int, unsigned int);
void join_channel(char *, struct User *, unsigned int);

/* ircd.c */
void InitMsgHash(void);
void parse();
void notice(char *, const char *, char *, ...);
void send_help_file(char *, char *, char *, char *);
void wallops(char *, char *, ...);
int flood(struct User *);
void SendQuit(struct User *, int, char *);

/* statserv.c */
#ifdef STATSERV
void statserv(struct User *, char *, char *);
#endif

/* servers.c */
extern dlink_list serverhash[S_TABLE_SIZE];
struct Server *AddServer(char *, int, int, char *, struct Server *);
void FlushServer(struct Server *);
void DelServer(char *);
void DelServerS(struct Server *);
struct Server *findserver(char *);
void init_server_hash();
void IncreaseServers();

/* channels.c */
struct Channel *chanlist[C_TABLE_SIZE];
struct Channel *AddChannel(char *, long);
void DelChannel(struct Channel *);
struct Channel *findchannel(char *);
void init_channel_hash();
void IncreaseChannels();
#ifdef IRCD_BAHAMUT
void ChannelMode(struct Channel *, char *, int, char **);
#else
void ChannelMode(struct Channel *, int, char **);
#endif
char *ModeString(struct Channel *);
void AddUserToChannel(struct Channel *, char *);
void AddUserFlagToChannel(struct Channel *, struct User *, unsigned int);
char *CUModeString(struct ChanUser *);
struct ChanUser *findchanuser(struct Channel *, struct User *);
void DelUserFromChannel(struct Channel *, struct User *);
void UnlinkChannelFromUser(struct Channel *, struct User *);
void HandleTSChange(struct Channel *, time_t);

#ifdef MONSERV 
/* hosthash.c */
struct HashEnt *hostlist[H_TABLE_SIZE];
struct HashEnt *domainlist[H_TABLE_SIZE];
struct HashEnt *uhlist[H_TABLE_SIZE];
void AddUserToHashes(struct User *);
void DelUserFromHashes(struct User *);
struct HashEnt *findhashent(struct HashEnt **, char *);
void init_host_hashes(void);
void get_domain(char *, char *);
#ifdef MON_DRONES
void CheckDrones(struct User *);
void CheckGecosDrones(struct User *, char *);
void CheckChannelDrones(struct User *, struct Channel *);
#ifdef TRACK_CTCPVERSION
void CheckVersionDrones(struct User *, char *);
#endif
void RemoveDrone(struct User *);
void RemoveDroneDL(dlink_node *);
#endif
#endif

/* users.c */
extern dlink_list userhash[U_TABLE_SIZE];
struct User *AddUser(char *, char *, char *, struct Server *, char *);
void DelUser(char *);
void DelUserU(struct User *);
#if defined(IRCD_BAHAMUT) || defined(IRCD_HYBSVS)
void Change_User(struct User *, char *, int);
#else
void Change_User(struct User *, char *);
#endif
#ifdef SUPPORT_HOSTSERV
void Change_Host(struct User *, char *);
void Change_Ident(struct User *, char *);
#endif
struct User *finduser(char *);
void UserMode(struct User *, char *);
void init_user_hash();
void IncreaseUsers(struct Server *);

/* myusers.c */
dlink_list myuhead;
dlink_list myudying;
struct MyUser *findmyuser(char *);

/* tld.c */
struct TLD *tldlist[T_TABLE_SIZE];
struct TLD *findtld(char *);
struct TLD *AddTLD(struct User *);
void LoadTLD();
void init_tld();

/* stats.c */
dlink_list shead;
dlink_list hiddenhead;
void IncreaseAway(struct Server *);
void IncreaseOpers(struct Server *);
void AddStats(struct Server *);
struct Stats *findstats(char *);
void SaveStats();
void LoadStats();
void AddHidden(char *, int);
void DelHidden(char *);
struct Hidden* findhidden(char *);

/* html.c */
void DumpHTML();

/* datadump.c */
void datadump();

#ifdef LISTSERV
/* listserv.c */
void listserv(struct User *, char *);
#endif

#ifdef JUPESERV

dlink_list server_jupes;
dlink_list nick_jupes;

/* jupe.c */
void LoadJupes();
void SaveJupes();
void AddServerJupe(char *, char *, long, char *, int);
void AddNickJupe(char *, char *, long, char *);
void InitServerJupe(struct Jupe *, int);
void InitNickJupe(struct Jupe *);
void DelServerJupe(char *);
void DelNickJupe(char *);
struct Jupe* FindServerJupe(char *);
struct Jupe* FindNickJupe(char *);
void FlushJupeList();

/* jupeserv.c */
void jupeserv(struct User *, char *);
void expire_pending_jupe_votes(void);

#endif

#ifdef MONSERV 
/* monserv.c */
void monserv(struct User *, char *);
#endif

/* info.c - autogenerated */
char *stats_version;
char *stats_date;

/* evh.c */
void StartInitialEvents(void);
void FlushEvents(void);
void ResetTimedEvents(void);
#if defined(SPYMODE) || defined(ALERTMODE) || defined(DRONES_CHANNEL) 
/* To deal with KILL's against the administration service, this needs to
** be exported
*/
void evh_CheckAdDelayed(void *unused);
#endif
#if defined(TRACK_CTCPVERSION)
void evh_CheckMnDelayed(void *unused);
#endif
void evh_ConnectServer(void *unused);

/* operlist.c */
dlink_list global_oper_list;
void AddOperToList(dlink_list *, struct User *);
void DelOperFromList(dlink_list *, struct User *);

/* connection.c */
dlink_list connection_list;
dlink_list dying_list;
struct Connection *add_connection(int, char *, unsigned int);
void delete_connection(int);
void dying_connection(int);
struct Connection *find_connection(int);

/* dcc.c */
void InitDCCHash(void);
void dcc_parse(struct Connection *, char *);
#ifdef ENABLE_TAKEOVER
dlink_list takeover_list;
#endif

/* capab.c */
char *generate_capab_string(void);

/* htmlsup.c */
void InitHTMLArray(void);
char *HTMLReplace(char *);
void PrintTopChannels(FILE *, int);

/* pubstats.c */
#ifdef PUBSTATS
void pubstats(struct User *, char *, char *);
#endif

/* challenge.c */
#ifdef HAVE_LIBCRYPTO
int generate_challenge(char **, char **, RSA *);
#endif

#ifdef MEMDEBUG
extern char *malloc_options;
#endif

#endif
